package com.shycloud.mido.music.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.music.domain.MisHotHistory;
import com.shycloud.mido.music.dto.MisMusicCollectionDTO;
import com.shycloud.mido.music.service.MisHotHistoryService;
import com.shycloud.mido.music.service.MisMusicCollectionService;
import com.shycloud.mido.music.service.MisMusicService;
import com.shycloud.mido.music.vo.MisMusicVO;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 曲目搜索模块 controller
 *
 * @author nianhua.jiang
 * @description
 * @date 2020/7/1
 */
@RestController
@AllArgsConstructor
@RequestMapping("/search")
@ApiOperation(value = "曲目搜索模块")
@ApiIgnore
public class SearchMusicController {

	/** 历史搜索与热门搜索列表 */
	private MisHotHistoryService hotHistoryService;

	private MisMusicService musicService;

	private MisMusicCollectionService collectionService;

	/**
	 * 获取热门搜索与历史搜索
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/1 16:20
	 */
	@ApiOperation("获取热门搜索与历史搜索")
	@GetMapping("getHotAndHistory")
	public R getHotAndHistory() {

		Map<String, Object> resultMap = MapUtil.newHashMap();

		//热门搜索
		List<MisHotHistory> hotSearchList = hotHistoryService.list(new QueryWrapper<MisHotHistory>()
				.eq(MisHotHistory.COL_TYPE, "1"));
		resultMap.put("hotSearch", hotSearchList);

		//历史搜索
		if (SecurityUtils.getMember() != null) {
			List<MisHotHistory> historySearchList = hotHistoryService.list(new QueryWrapper<MisHotHistory>()
					.eq(MisHotHistory.COL_TYPE, "2")
					.eq(MisHotHistory.COL_MEMBER_ID, SecurityUtils.getMember().getId())
					.orderByDesc(MisHotHistory.COL_CREATE_TIME));
			resultMap.put("historySearch", historySearchList);
		} else {
			resultMap.put("historySearch", new ArrayList<MisHotHistory>());
		}

		return new R(resultMap);
	}

	/**
	 * 搜索曲集/曲目
	 *
	 * @param keywords
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/1 18:39
	 */
	@ApiOperation("搜索曲集/曲目")
	@GetMapping("searchMusic")
	public R searchMusic(Integer current, @RequestParam(name = "size") Integer size, String keywords, String flg) {

		//注: flg = isCustom null代表正常搜索 1代表定制搜索

		//分页参数
		Page page1 = new Page(current, 200000);
		Page page2 = new Page(current, 200000);

		//相关曲集搜索
		IPage<MisMusicCollectionDTO> collectionResult = hotHistoryService.searchCollection(page1, keywords, flg);

		//相关曲目搜索
		IPage<MisMusicVO> musicResult = null;

		//TODO 0426新增需求 定制曲集也要搜索定制曲集下的曲目
		if ("3".equals(flg)) {
			//musicResult = hotHistoryService.searchMusic(page2, "93jdkajdakjn dasjnadnjkaopdadojd");
			musicResult = new Page<MisMusicVO>(1, 1);
		} else {
			musicResult = hotHistoryService.searchMusic(page2, keywords, flg);
		}

		if (SecurityUtils.getMember() != null) {
			addMisHotHistory(keywords);
		}

		//初始化结果集
		Map<String, Object> resultMap = MapUtil.newHashMap();

		//TODO 笨方法解决保证好用
		if ("1".equals(flg)) {
			//搜索曲目对应的曲集放入返回体中
			List<MisMusicVO> records = musicResult.getRecords();
			List<MisMusicCollectionDTO> misMusicCollRes = new ArrayList<>();
			for (MisMusicVO record : records) {

			}
		}

		//搜索曲集结果
		resultMap.put("collection", collectionResult);
		//搜索曲目结果
		resultMap.put("music", musicResult);

		return new R(resultMap);
	}

	/**
	 * 搜索曲集/曲目 小程序端
	 *
	 * @param keywords
	 * @param current
	 * @param size
	 * @param flg      3-只搜曲集，4-只搜曲目
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2021/3/22 16:25
	 */
	@ApiOperation("搜索曲集/曲目")
	@GetMapping("searchMusicMini")
	public R searchMusicMini(Integer current, Integer size, String keywords, String flg) {

		//分页参数
		Page page = new Page(current, size);

		//相关曲集搜索
		if ("3".equals(flg)) {
			return new R(hotHistoryService.searchCollection(page, keywords, flg));
		}

		//曲目搜索
		if ("4".equals(flg)) {
			return new R(hotHistoryService.searchMusic(page, keywords, null));
		}

		if (SecurityUtils.getMember() != null) {
			addMisHotHistory(keywords);
		}

		return new R(CommonConstants.FAIL, "flg参数有误", false);
	}


	/**
	 * 删除历史搜索
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/2 9:54
	 */
	@GetMapping("delSearch")
	@ApiOperation("删除历史搜索")
	public R delMisHotHistory() {
		return new R(hotHistoryService.remove(new QueryWrapper<MisHotHistory>().eq(MisHotHistory.COL_MEMBER_ID, SecurityUtils.getMember().getId())));
	}

	/**
	 * 添加搜索历史数据
	 *
	 * @param keyword
	 * @return void
	 * @author nianhua.jiang
	 * @date 2020/7/1 19:58
	 */
	private void addMisHotHistory(String keyword) {

		//保存搜索的关键字信息
		MisHotHistory misHotHistory = hotHistoryService.getOne(new QueryWrapper<MisHotHistory>()
				.eq(MisHotHistory.COL_MEMBER_ID, SecurityUtils.getMember().getId())
				.and(x -> x.eq(MisHotHistory.COL_KEYWORDS, keyword))
				.and(y -> y.eq(MisHotHistory.COL_TYPE, "2")));

		if (misHotHistory == null) {
			misHotHistory = new MisHotHistory();
			//关键字
			misHotHistory.setKeywords(keyword);
			//类型：历史搜索
			misHotHistory.setType("2");
			//用户Id
			misHotHistory.setMemberId(SecurityUtils.getMember().getId());
			//时间
			misHotHistory.setCreateTime(LocalDateTime.now());
			hotHistoryService.save(misHotHistory);
		} else {
			misHotHistory.setSearchCount(misHotHistory.getSearchCount() + 1);
			hotHistoryService.updateById(misHotHistory);
		}

		List<MisHotHistory> misHotHistories = hotHistoryService.list(new QueryWrapper<MisHotHistory>()
				.eq(MisHotHistory.COL_MEMBER_ID, SecurityUtils.getMember().getId())
				.orderByDesc(MisHotHistory.COL_CREATE_TIME));

		if (misHotHistories.size() > 10) {
			for (int i = 10; i < misHotHistories.size(); i++) {
				hotHistoryService.removeById(misHotHistories.get(i).getId());
			}
		}
	}
}
